package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.UserDao;
import cn.edu.sdjzu.xg.bysj.domain.User;
import cn.edu.sdjzu.xg.bysj.domain.authority.Actor;
import lombok.extern.slf4j.Slf4j;
import util.Condition;
import util.JWTHelper;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;

@Slf4j
public final class UserService {
	//获取UserDao实例
	private UserDao userDao = UserDao.getInstance();
	//新建userService对象
	private static UserService userService = new UserService();
	//构造器
	public UserService() {
	}
	//获取userService对象
	public static UserService getInstance(){
		return UserService.userService;
	}
	/**
	 * 查找
	 * @param conditions
	 * @param pagination
	 * @return
	 * @throws SQLException
	 */
	public Collection<User> findAll(Collection<Condition> conditions , Pagination pagination) throws SQLException {
		//获取数据库链接
		Connection connection = JdbcHelper.getConn();
		Collection<User> users = null;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			users = userDao.findAll(connection,conditions,pagination);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return users;
	}
	/**
	 * 获取
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public User getUser(Integer id) throws SQLException {
		//获取数据库链接
		Connection connection = JdbcHelper.getConn();
		User user = null;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			user = UserDao.getInstance().find(connection,id);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return user;
	}
	/**
	 * @param user
	 * @return
	 * @throws SQLException
	 */
	public boolean update(User user) throws SQLException {
		//获取数据库链接
		Connection connection = JdbcHelper.getConn();
		boolean updated = false;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			updated = userDao.update(connection,user);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return updated;
	}
	/**
	 * 新增
	 * @param user
	 * @return
	 * @throws SQLException
	 */
	public boolean add(User user) throws SQLException {
		//获取数据库链接
		Connection connection = JdbcHelper.getConn();
		boolean added = false;
		added = userDao.add(connection,user);
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			added = userDao.add(connection,user);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return added;
	}
	/**
	 * 删除
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public boolean delete(Integer id) throws SQLException {
		//获取数据库链接
		Connection connection = JdbcHelper.getConn();
		boolean deleted = false;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			deleted = userDao.delete(connection,id);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return deleted;
	}

	/**
	 * 登录
	 * @param userToLogin
	 * @return
	 * @throws SQLException
	 */
	public Actor login(User userToLogin) throws SQLException {
		HashMap<String,String> payload = new HashMap<>();
		payload.put("username",userToLogin.getUsername());
		payload.put("password",userToLogin.getPassword());
		String token = JWTHelper.getToken(payload);
		Connection connection = JdbcHelper.getConn();
		Actor actor = userDao.login(connection, userToLogin);
		JdbcHelper.close(connection);
		actor.setToken(token);
		return actor;
	}
}
